/*==============================================================================
================================================================================

File to create tables in Plueger, Siriwardane, and Sunderam (2020)
"Financial Market Risk Perceptions and the Macroeconomy" 
 
================================================================================ 
==============================================================================*/	


/*------------------------------------------------------------------------------

Define environment
 
------------------------------------------------------------------------------*/

clear
clear matrix
clear mata
set maxvar 10000
graph drop _all
graph close _all

use "pss_data_replication.dta", clear
tsset yq

log using pss_replication.smcl, replace

/*------------------------------------------------------------------------------

Table 1 - Summary Statistics
 
------------------------------------------------------------------------------*/

sum real_rate_1yr,d 
sum ldt_real_rate_1yr, d
sum pvs, d
sum tvol_BM_Star_ew_ret, d


/*------------------------------------------------------------------------------

Table 2 - PVS and Expectations of Risk
 
------------------------------------------------------------------------------*/


/*------------------------------------------------------------------

Use AR(1) to forecast avg volatility of high versus low volatility
 
--------------------------------------------------------------------*/

*Generate high-minus-low, instead of low-minus-high
gen hml_avgvol = -tvol_BM_Star_avgvol_ew_spr

qui arima hml_avgvol, ar(1)
predict mean_avg_vol, xb

*Get forward prediction, which is in time-t information set
gen e_hml_avgvol = F.mean_avg_vol


/*------------------------------------------------------------------

Now run regressions
 
--------------------------------------------------------------------*/

*Standardize all varibles.  Control for growth expectations in second regression
egen std_meanest_ltg_spr = std(meanest_ltg_spr)

local i 1

* Analyst dispersion - two fiscal periods away
egen std_disp_ann_spr = std(disp_ann_spr)
qui neweymod std_pvs  std_disp_ann_spr, lag(5)
est store m`i++'
qui neweymod std_pvs std_meanest_ltg_spr std_disp_ann_spr, lag(5)
est store m`i++'

* Analyst dispersion - one quarter away
egen std_disp_qtr_spr = std(disp_qtr_spr)
qui neweymod std_pvs std_disp_qtr_spr, lag(5)
est store m`i++'
qui neweymod std_pvs std_meanest_ltg_spr std_disp_qtr_spr, lag(5)
est store m`i++'

* 365-day implied volatility.  Drop prior to 1996Q3 because sparsely populated
replace iv365_spr = . if yq<yq(1996,3)
egen std_iv365_spr = std(iv365_spr)
qui neweymod std_pvs std_iv365_spr, lag(5)
est store m`i++'
qui neweymod std_pvs std_meanest_ltg_spr std_iv365_spr, lag(5)
est store m`i++'

* Expected Average volatility in low vol minus average vol in high vol
egen std_e_hml_avgvol = std(e_hml_avgvol)
qui neweymod std_pvs std_e_hml_avgvol, lag(5)
est store m`i++'
qui neweymod std_pvs std_meanest_ltg_spr std_e_hml_avgvol, lag(5)
est store m`i++'

*Average analyst dispersion across all portfolios - two fiscal periods away
egen std_disp_ann_all = std((disp_ann1 + disp_ann2 + disp_ann3 + disp_ann4 ///
+ disp_ann5) / 5)
qui neweymod std_pvs std_disp_ann_all, lag(5)
est store m`i++'
qui neweymod std_pvs std_meanest_ltg_spr std_disp_ann_all, lag(5)
est store m`i++'

* SLOOS loosening
gen prc_banks_loosen = -prc_banks_tighten 
egen std_prc_banks_loosen = std(prc_banks_loosen)
qui neweymod std_pvs std_prc_banks_loosen, lag(5)
est store m`i++'
qui neweymod std_pvs std_meanest_ltg_spr std_prc_banks_loosen, lag(5)
est store m`i++'

* SLOOS risk tolerance
egen std_prc_loose_outlook = std(prc_loose_outlook)
qui neweymod std_pvs std_prc_loose_outlook , lag(5)
est store m`i++'
qui neweymod std_pvs std_meanest_ltg_spr std_prc_loose_outlook , lag(5)
est store m`i++'

*Small business optimism
egen std_smallbiz = std(smallbusinessoptimism)
qui neweymod std_pvs std_smallbiz , lag(5)
est store m`i++'
qui neweymod std_pvs std_meanest_ltg_spr std_smallbiz , lag(5)
est store m`i++'

*Baker et al (2016) economic policy uncertainty index
egen std_bloom = std(bloom_uncertainty)
qui neweymod std_pvs std_bloom , lag(5)
est store m`i++'
qui neweymod std_pvs std_meanest_ltg_spr std_bloom , lag(5)
est store m`i++'

est table * , se stats(r2_a N) 
est drop _all


/*------------------------------------------------------------------------------

Table 3
 
------------------------------------------------------------------------------*/

local i 1 

***** Levels *****
*y = RR, X = PVS
qui neweymod ldt_real_rate_1yr std_pvs, lag(5) 
est store m`i++'

*y = RR, X = [PVS, Agg BM]
qui neweymod ldt_real_rate_1yr std_pvs std_ldt_agg_bm, lag(5) 
est store m`i++'

*y = RR, X = BM Low Vol, BM High Vol
qui neweymod ldt_real_rate_1yr std_tvol_BM_Star_ew1 std_tvol_BM_Star_ew5, lag(5) 
est store m`i++'

*y = RR, X = [PVS, BM_Star_agg_mkt, Taylor Rule Variables]
qui neweymod ldt_real_rate_1yr std_pvs std_ldt_agg_bm ///
ldt_output_gap ldt_inflation, lag(5) 
est store m`i++'

***** Q-Q Changes *****
*y = RR, X = PVS
qui neweymod s.ldt_real_rate_1yr std_s_pvs, lag(5) 
est store m`i++'

*y = RR, X = [PVS, Agg BM]
qui neweymod s.ldt_real_rate_1yr std_s_pvs std_s_ldt_agg_bm, lag(5) 
est store m`i++'

*y = RR, X = BM Low Vol, BM High Vol
qui neweymod s.ldt_real_rate_1yr std_s_tvol_BM_Star_ew1 std_s_tvol_BM_Star_ew5, lag(5) 
est store m`i++'

*y = RR, X = [PVS, BM_Star_agg_mkt, Taylor Rule Variables]
qui neweymod s.ldt_real_rate_1yr std_s_pvs std_s_ldt_agg_bm ///
s.ldt_output_gap s.ldt_inflation, lag(5) 
est store m`i++'

est table * , se stats(r2_a N) 
est drop _all


/*------------------------------------------------------------------------------

Table 4: Robusntess
 
------------------------------------------------------------------------------*/


/*---------------------------------------------------------
Table 4A - Other Financial Market Conditions
----------------------------------------------------------*/

local i 1

*Now run through each alternative measure
foreach v in  baa_t10y gz_spr  ///
HighYieldShareMA4 SENT_ORTH mrp_IS bloom_uncertainty {

egen std_`v' = std(`v')

qui neweymod std_pvs std_`v', lag(5) 
est store m`i++'

qui neweymod ldt_real_rate_1yr std_`v', lag(5) 
est store m`i++'

qui neweymod ldt_real_rate_1yr std_`v' std_pvs, lag(5) 
est store m`i++'

}

est table * , se stats(r2_a N) 
est drop _all


/*---------------------------------------------------------
Table 4B - Monetary Policy Surprises
----------------------------------------------------------*/

*Save current workfile for later use
tempfile main_file
save `main_file', replace

do mp_shocks_analysis_clean.do
use `main_file', clear

/*------------------------------------------------------------------------------

Table 5 - ROE Forecasting
 
------------------------------------------------------------------------------*/

*Note: Return forecasting done in forecast_returns.py for Hodrick (1992) SEs.

local i 1

***** ROE Forecasting *****
replace tvol_BM_Star_ew_roe = tvol_BM_Star_ew_roe * 100

*y = LMH Vol Roe(t+1 to t+4), X = PVS(t)
qui neweymod f4.tvol_BM_Star_ew_roe std_pvs, lag(5) 
est store m`i++'

* y = LMH Vol Roe(t+1 to t+4), X = RR(t)
qui neweymod f4.tvol_BM_Star_ew_roe ldt_real_rate_1yr, lag(5) 
est store m`i++'

***** Output *****
est table * , se stats(r2_a N) 
est drop _all



/*------------------------------------------------------------------------------

Table 6 - PVS and the Macroeconomy
 
------------------------------------------------------------------------------*/

/*---------------------------------------------------------
Jorda Local Projections
----------------------------------------------------------*/

local i 1

* Number of lags
local MaxLPLags 0

* Horizon
local horizon 4

**** Controls in LP regression ****
local rhsinvestir l(0/`MaxLPLags').std_pvs ///
					l(0/`MaxLPLags').ik ///
					l(0/`MaxLPLags').ldt_real_rate_1yr
					
local rhsinvestirmkt l(0/`MaxLPLags').std_ldt_agg_bm ///
					l(0/`MaxLPLags').ik ///
					l(0/`MaxLPLags').ldt_real_rate_1yr
					
						
local rhsoutputgapir l(0/`MaxLPLags').std_pvs ///
				l(0/`MaxLPLags').output_gap ///
				l(0/`MaxLPLags').ldt_real_rate_1yr
				
local rhsoutputgapir_mkt l(0/`MaxLPLags').std_ldt_agg_bm ///
				l(0/`MaxLPLags').output_gap ///
				l(0/`MaxLPLags').ldt_real_rate_1yr				
				
local rhsunrateir l(0/`MaxLPLags').std_pvs ///
					l(0/`MaxLPLags').d_unrate_qq ///
					l(0/`MaxLPLags').ldt_real_rate_1yr
					
local rhsunrateir_mkt l(0/`MaxLPLags').std_ldt_agg_bm ///
					l(0/`MaxLPLags').d_unrate_qq ///
					l(0/`MaxLPLags').ldt_real_rate_1yr
									
					
forvalues j=1/`horizon' {	
	
	*Cumulative effect on investment, real GDP, Ind Pro, and unemployment
	qui gen invgrowth`j' = (f`j'.pnfic1/pnfic1)*100
	qui gen dunrate`j' = f`j'.unrate - unrate

	
	** LP regressions **
	* Real private fixed non-residential investment
	qui neweymod f`j'.ik std_pvs `rhsinvestir', lag(5)
	est store inv`i'
	
	* Real private fixed non-residential investment using aggregate mkt
	qui neweymod f`j'.ik std_ldt_agg_bm `rhsinvestirmkt', lag(5)
	est store mktinv`i'
		
	* Output gap
	qui neweymod f`j'.output_gap std_pvs `rhsoutputgapir', lag(5)
	est store og`i'
	
	* Output gap using aggregate mkt
	qui neweymod f`j'.output_gap std_ldt_agg_bm `rhsoutputgapir_mkt', lag(5)
	est store mktog`i'	
		
	* Unemployment Rate
	qui neweymod dunrate`j' std_pvs `rhsunrateir', lag(5)
	est store ur`i'
	
	* Unemployment Rate using aggregate mkt
	qui neweymod dunrate`j' std_ldt_agg_bm `rhsunrateir_mkt', lag(5)
	est store mktur`i++'	
	
}

est table *1 *4 , se stats(r2_a N) 
est drop _all


/*------------------------------------------------------------------------------

Table 7 - PVS and Good News
 
------------------------------------------------------------------------------*/

* Standardize four-quarter change in PVS
egen std_s4_pvs = std(s4.pvs)

local i 1

*GDP surprises
egen rGDP_surprise = std((g_realGDP_ann - l4.g_exp_rgdp))
qui neweymod std_s4_pvs rGDP_surprise, lag(5)
est store m`i++'

*Corporate profit growth surprises
egen cprof_surprise =  std((g_nipa_cpbt_ann - l4.g_exp_cprof))
qui neweymod std_s4_pvs cprof_surprise , lag(5)
est store m`i++'

*Realized ROE
egen std_tvol_BM_Star_ew_roe = std(tvol_BM_Star_ew_roe)
qui neweymod std_s4_pvs std_tvol_BM_Star_ew_roe, lag(5)
est store m`i++'

*Chargeoffs
egen s4_net_chargeoff_rate = std((s4.net_chargeoff_rate))
qui neweymod std_s4_pvs s4_net_chargeoff_rate, lag(5)
est store m`i++'

*All variables
qui neweymod std_s4_pvs rGDP_surprise cprof_surprise ///
std_tvol_BM_Star_ew_roe s4_net_chargeoff_rate, lag(5)
est store m`i++'

est table * , se stats(r2_a N)  
est drop _all



/*------------------------------------------------------------------------------

Table 8 - Forecasting Revisions in Growth and Risk Expectations
 
------------------------------------------------------------------------------*/


* Once again standardize variables to get sense of economic magnitude

local i 1


*Future changes in analyst dispersion - quarterly earnings.  
*Drop prior to 1993Q1 b/c not continuous data
replace f2s2_disp_qtr3_spr = . if yq<yq(1993,1)
egen std_f2s2_disp_qtr3_spr = std(f2s2_disp_qtr3_spr)
qui neweymod std_f2s2_disp_qtr3_spr std_pvs , lag(5)
est store m`i++'


*Future innovations to option-implied volatility.  Irregular data <= 1996Q2
replace volnews_9m_9m_1y_spr = . if yq<yq(1996,3)
egen std_volnews_9m_9m_1y_spr = std(volnews_9m_9m_1y_spr )
qui neweymod std_volnews_9m_9m_1y_spr std_pvs , lag(5)
est store m`i++'

*Future changes in percent of banks loosening
egen std_f4s4_prc_banks_loosen = std(f4s4.prc_banks_loosen)
qui neweymod std_f4s4_prc_banks_loosen std_pvs , lag(5)
est store m`i++'

*Future changes in realized market volatility
egen std_f4s4_mktrf_vol = std(f4s4.mktrf_vol)
qui neweymod std_f4s4_mktrf_vol std_pvs, lag(5)
est store m`i++'

*Future changes in the realized avg portfolio vol
egen std_f4s4_total_vol_spr = std(-f4s4_total_vol_spr)
qui neweymod std_f4s4_total_vol_spr std_pvs, lag(5)
est store m`i++'


est table * , se stats(r2_a N) 
est drop _all


/*------------------------------------------------------------------------------

Table 9 - PVS and Implied Volatility Forecast Errors
 
------------------------------------------------------------------------------*/
/*
do iv_error_analysis.do
*/

log close
